﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Collections.Specialized;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using Studio.Network.Mail;

////////////
/// 
/// Duplicated to System.Net.Mail
/// By marstone@marstone.net, 2008/12/02
/// 
////////////
namespace Studio.Network.MailServer
{

    public class SmtpData
    {
        EmailElements _elmenent;

        public EmailElements Elmenent
        {
            get { return _elmenent; }
            set { _elmenent = value; }
        }

        string _content;

        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }

        public static string TERMINATION_SYMBOL = "\r\n.\r\n";

        public SmtpData(string content)
        {
            this.ParseContent(content);
        }

        string _data = string.Empty;

        /// <summary>
        /// Mapping from SmtpVerb to its Prefix
        /// </summary>
        public static readonly Dictionary<EmailElements, string> EMAIL_HEADER_DICTIONARY = new Dictionary<EmailElements, string>() 
        { 
            
            {EmailElements.FROM, "FROM:"},
            {EmailElements.TO, "TO:"},
            {EmailElements.BCC, "BCC:"},
            {EmailElements.CC, "CC:"},
            {EmailElements.SUBJECT, "SUBJECT:"}
        };

        void ParseContent(string content)
        {
            this._content = content;
            this._elmenent = EmailElements.UNSUPPORTED;
            foreach (EmailElements element in EMAIL_HEADER_DICTIONARY.Keys)
            {
                string prefix = EMAIL_HEADER_DICTIONARY[element];
                if (true == content.ToUpper().StartsWith(prefix))
                {
                    this._elmenent = element;
                    this._data = content.Substring(prefix.Length - 1).Trim(" \r\n".ToCharArray());
                    break;
                }
            }
        }

        public static SmtpData Parse(string content)
        {
            return new SmtpData(content);
        }
    }
}
